//
//  KZtabbarTitle.m
//  开源中国
//
//  Created by kun on 15/10/4.
//  Copyright (c) 2015年 kun. All rights reserved.
//

#import "KZBarTitle.h"
#import "UIColor+Util.h"


#define SELECTCOLOR 0x000000

@interface KZBarTitle ()

@property (nonatomic,assign) NSInteger curretBtnIndex;

//控制底部滑动
@property (nonatomic,weak) UIView *indicatorView;


@property (nonatomic,strong) NSArray *indicatorArray;


@end


@implementation KZBarTitle



-(instancetype)initWithFrame:(CGRect)frame andTitles:(NSArray *)titles
{
    if(self = [super initWithFrame:frame])
    {
        //1 初始化titles
    
        
        _titleButtons = [NSMutableArray array];
        CGFloat W =  frame.size.width/titles.count;
        CGFloat H =  frame.size.height;
        
        
        UIView *indicV = [[UIView alloc]initWithFrame:CGRectMake(0, 43, W, 2)];
        indicV.backgroundColor = [UIColor colorWithHex:SELECTCOLOR];
        [self addSubview:indicV];
        self.indicatorView = indicV;
        
        
        
        //初始化指针数组
        NSMutableArray *indicArrayM = [NSMutableArray arrayWithCapacity:2];
        
        for(int i = 0 ; i < titles.count; i ++)
        {
            UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
            button.backgroundColor = [UIColor titleBarColor];
            button.titleLabel.font = [UIFont systemFontOfSize:15];
            [button setTitleColor:[UIColor colorWithHex:0x909090] forState:UIControlStateNormal];
            [button setTitle:titles[i] forState:UIControlStateNormal];
            
            button.frame = CGRectMake(W*i,0, W, H);
           // NSLog(@"--->%@   bouns : %@  . frame : %@",titles[i],  NSStringFromCGRect(self.bounds),NSStringFromCGRect(button.frame));
            
            button.tag = i;
            [button addTarget:self action:@selector(dealBtn:) forControlEvents:UIControlEventTouchUpInside];
            [self addSubview:button];
            [_titleButtons addObject:button];
            
            [button setBackgroundColor:[UIColor clearColor]];
            
            //button添加下划线
            UIView *indicatorView = [[UIView alloc]initWithFrame:CGRectMake(W*i, 43, W, 2)];
            indicatorView.backgroundColor = [UIColor colorWithHex:SELECTCOLOR];
            [button addSubview:indicatorView];
            [indicArrayM addObject:indicatorView];
        }
        
        _indicatorArray = indicArrayM;
        
        //设置默认值
       // _curretBtnIndex =  NSNotFound;
        
        //2设置scrllView的contnentSize大小
        self.contentSize = CGSizeMake(frame.size.width, 0);
        self.showsHorizontalScrollIndicator = NO;
        
        //记录当前的的btn 并初始化默认第一个
        UIButton * curretBtn = _titleButtons[0];
        [curretBtn setTitleColor:[UIColor colorWithHex:SELECTCOLOR] forState:UIControlStateNormal];
        
        //默认选择第一个
        _indicatorView.hidden = YES;
        
    }

    return self;
}



-(void)dealBtn:(UIButton *)btn
{
    if(_curretBtnIndex == btn.tag) return;
    
    
    //这里使用了文字的形变属性 之前的恢复
    UIButton * preBtn = _titleButtons[_curretBtnIndex];
    
    [preBtn setTitleColor:[UIColor colorWithHex:0x909090] forState:UIControlStateNormal];
    preBtn.transform = CGAffineTransformIdentity;
    
    //获取选中的位置
    //_indicatorView
    NSInteger index = [_titleButtons indexOfObject:btn];
    UIView *preIndicatorView = _indicatorArray[_curretBtnIndex];
    preIndicatorView.hidden = YES;
    self.indicatorView.hidden = NO;
    
    [UIView animateWithDuration:0.5 animations:^{
       self.indicatorView.frame =  ((UIView *)_indicatorArray[index]).frame;
    } completion:^(BOOL finished) {
        //self.indicatorView.hidden = YES;
        ((UIView *)_indicatorArray[index]).hidden = NO;
    }];
    
    
    
    
    //2 改变选中以后的图形
    [btn setTitleColor:[UIColor colorWithHex:SELECTCOLOR] forState:UIControlStateNormal];
   // btn.transform = CGAffineTransformMakeScale(1.2, 1.2);
    _curretBtnIndex = btn.tag;
    
    //3 这里实现一个block 或者代理事件
    if(_tabbarTitleChange)
    {
        self.tabbarTitleChange(btn,btn.tag);
    }
}

//block用于外界交互
-(void)changeButtonWithIndex:(NSInteger )index
{
    
     if(_curretBtnIndex == index) return;
    //这里使用了文字的形变属性 之前的恢复
    UIButton * preBtn = _titleButtons[_curretBtnIndex];
    
    [preBtn setTitleColor:[UIColor colorWithHex:0x909090] forState:UIControlStateNormal];
    preBtn.transform = CGAffineTransformIdentity;
    
    
    
    UIView *preIndicatorView = _indicatorArray[_curretBtnIndex];
    preIndicatorView.hidden = YES;
    self.indicatorView.hidden = NO;
    [UIView animateWithDuration:0.2 animations:^{
        self.indicatorView.frame =  ((UIView *)_indicatorArray[index]).frame;
    } completion:^(BOOL finished) {
        //self.indicatorView.hidden = YES;
        ((UIView *)_indicatorArray[index]).hidden = NO;
    }];
    
    //2 改变选中以后的图形
    UIButton * btn = _titleButtons[index];

    [btn setTitleColor:[UIColor colorWithHex:SELECTCOLOR] forState:UIControlStateNormal];
   // btn.transform = CGAffineTransformMakeScale(1.2, 1.2);
    _curretBtnIndex = btn.tag;
    //[self dealBtn:button];
}


//- (void)willMoveToSuperview:(UIView *)newSuperview
//{
//
//}
@end
